//
//  TTIGoalBarPercentLayer.m
//  iOSCodeProject
//
//  Created by Fox on 14-7-18.
//  Copyright (c) 2014年 翔傲信息科技（上海）有限公司. All rights reserved.
//

#import "TTIGoalBarPercentLayer.h"

#define toRadians(x) ((x)*M_PI / 180.0)
#define toDegrees(x) ((x)*180.0 / M_PI)

@implementation TTIGoalBarPercentLayer

@synthesize percent = _percent;
@synthesize innerRadius = _innerRadius;
@synthesize outerRadius= _outerRadius;
@synthesize fillColor = _fillColor;
@synthesize defaultColor = _defaultColor;


-(void)drawInContext:(CGContextRef)ctx {
    
    if (self.fromBottumCenter) {
        [self DrawRight:ctx];
        [self DrawLeft:ctx];
    }else{
        [self DrawUp:ctx];
        [self DrawDown:ctx];
    }
}

#pragma mark - 从底部中间为0开始计算
-(void)DrawRight:(CGContextRef)ctx {
    
    CGPoint center = CGPointMake(self.frame.size.width / (2), self.frame.size.height / (2));
    
    CGFloat delta = -toRadians(360 * _percent);
    
    CGContextSetFillColorWithColor(ctx, self.fillColor.CGColor);
    
    CGContextSetLineWidth(ctx, 1);
    
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    CGContextSetAllowsAntialiasing(ctx, YES);
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathAddRelativeArc(path, NULL, center.x, center.y, _innerRadius, -(M_PI / 2), delta);
    CGPathAddRelativeArc(path, NULL, center.x, center.y, _outerRadius, delta - (M_PI / 2), -delta);
    CGPathAddLineToPoint(path, NULL, center.x, center.y-_innerRadius);
    
    CGContextAddPath(ctx, path);
    CGContextFillPath(ctx);
    
    CFRelease(path);
}

-(void)DrawLeft:(CGContextRef)ctx {
    CGPoint center = CGPointMake(self.frame.size.width / (2), self.frame.size.height / (2));
    
    CGFloat delta = toRadians(360 * (1-_percent));
    
    CGContextSetFillColorWithColor(ctx, self.defaultColor.CGColor);
    
    CGContextSetLineWidth(ctx, 1);
    
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    CGContextSetAllowsAntialiasing(ctx, YES);
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathAddRelativeArc(path, NULL, center.x, center.y, _innerRadius, -(M_PI / 2), delta);
    CGPathAddRelativeArc(path, NULL, center.x, center.y, _outerRadius, delta - (M_PI / 2), -delta);
    CGPathAddLineToPoint(path, NULL, center.x, center.y-_innerRadius);
    
    CGContextAddPath(ctx, path);
    CGContextFillPath(ctx);
    
    CFRelease(path);
}

#pragma mark - 从左侧中间为0开始计算
-(void)DrawUp:(CGContextRef)ctx {
    
    CGPoint center = CGPointMake(self.frame.size.width / (2), self.frame.size.height / (2));
    
    CGFloat delta = toRadians(360 * _percent);
    
    CGContextSetFillColorWithColor(ctx, self.fillColor.CGColor);
    
    CGContextSetLineWidth(ctx, 1);
    
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    CGContextSetAllowsAntialiasing(ctx, YES);
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathAddRelativeArc(path, NULL, center.x, center.y, _innerRadius, -M_PI, delta);
    CGPathAddRelativeArc(path, NULL, center.x, center.y, _outerRadius, delta + M_PI  , -delta);
    CGPathAddLineToPoint(path, NULL, center.x - _innerRadius, center.y);
    
    CGContextAddPath(ctx, path);
    CGContextFillPath(ctx);
    
    CFRelease(path);
}

-(void)DrawDown:(CGContextRef)ctx {
    CGPoint center = CGPointMake(self.frame.size.width / (2), self.frame.size.height / (2));
    
    CGFloat delta = -toRadians(360 * (1-_percent));
    
    CGContextSetFillColorWithColor(ctx, self.defaultColor.CGColor);
    
    CGContextSetLineWidth(ctx, 1);
    
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    CGContextSetAllowsAntialiasing(ctx, YES);
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathAddRelativeArc(path, NULL, center.x, center.y, _innerRadius, -M_PI, delta);
    CGPathAddRelativeArc(path, NULL, center.x, center.y, _outerRadius, delta - M_PI, -delta);
    CGPathAddLineToPoint(path, NULL, center.x - _innerRadius, center.y);
    
    CGContextAddPath(ctx, path);
    CGContextFillPath(ctx);
    
    CFRelease(path);
}

@end
